﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class RssCloudSubsDataSource
    {
        private RssCloudSubsDataServiceContext _ServiceContext = null;

        public RssCloudSubsDataSource(CloudStorageAccount storageAccount)
        {
            //var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new RssCloudSubsDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(RssCloudSubsDataServiceContext.RssCloudSubsTableName);
        }

        public IEnumerable<RssCloudSubsDataModel> Select()
        {
            var results = from c in _ServiceContext.RssCloudSubsTable
                          orderby c.DateTime
                          select c;

            var query = results.AsTableServiceQuery<RssCloudSubsDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<RssCloudSubsDataModel> Select(string hash)
        //{
        //    var results = from c in Select()
        //                  where c.Hash==hash
        //                  select c;

        //    return results;
        //}

        //public IEnumerable<RssCloudSubsDataModel> SelectBlog(string id)
        //{
        //    var results = from c in Select()
        //                  where c.BlogID==id
        //                  select c;

        //    return results;
        //}

        //public IEnumerable<RssCloudSubsDataModel> WhereDate(DateTime date)
        //{
        //    var results = from c in Select()
        //                  where c.DateTime<date
        //                  select c;

        //    return results;
        //}
        //public void Delete(RssCloudSubsDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(RssCloudSubsDataServiceContext.RssCloudSubsTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}
        //public void DeleteRange(IEnumerable<RssCloudSubsDataModel> itemsToDelete)
        //{
        //    foreach (RssCloudSubsDataModel item in itemsToDelete)
        //    {
        //        Delete(item);
        //    }
        //}

        //public void Insert(RssCloudSubsDataModel newItem)
        //{
        //    _ServiceContext.AddObject(RssCloudSubsDataServiceContext.RssCloudSubsTableName, newItem);
        //    _ServiceContext.SaveChanges();
        //}
    }
}
